package com.tanhua.manager.service;

import com.tanhua.domain.db.AnalysisByDay;
import com.tanhua.dubbo.api.db.AnalysisByDayApi;
import com.tanhua.dubbo.api.db.LogApi;
import com.tanhua.manager.utils.ComputeUtil;
import com.tanhua.vo.AnalysisSummaryVo;
import org.apache.dubbo.config.annotation.Reference;
import org.joda.time.DateTime;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class AnalysisByDayService {

    @Reference
    private LogApi logApi;
    @Reference
    private AnalysisByDayApi analysisByDayApi;

    public void analysis(String todayStr) {
        AnalysisByDay analysisByDay = new AnalysisByDay();

        analysisByDay.setRecordDate(DateTime.parse(todayStr).toDate()); //记录时间

        Integer numRegistered = logApi.queryNumLoginOrRegistry(todayStr,"0102"); // 有多少人注册
        analysisByDay.setNumRegistered(numRegistered);

        Integer numLogin = logApi.queryNumLoginOrRegistry(todayStr,"0101"); //  有多少人登录
        analysisByDay.setNumLogin(numLogin);


        Integer numActive = logApi.queryNumActive(todayStr); //  有多少人登录
        analysisByDay.setNumActive(numActive);


        String yesTodayStr = DateTime.parse(todayStr).minusDays(1).toString("yyyy-MM-dd");

        Integer numRetention1d = logApi.queryNumRetention1d(todayStr,yesTodayStr); //  有多少人登录
        analysisByDay.setNumRetention1d(numActive);

//        判断今天是否有记录 如果有修改 如果没有新增
        AnalysisByDay analysisByDay_db = analysisByDayApi.queryByRecodDate(DateTime.parse(todayStr).toDate());
        if(analysisByDay_db!=null){
            analysisByDay.setId(analysisByDay_db.getId());
            analysisByDayApi.updateById(analysisByDay);
        }else{
            analysisByDayApi.save(analysisByDay);
        }





    }

    public ResponseEntity summary(String todayStr) {

        AnalysisSummaryVo analysisSummaryVo = new AnalysisSummaryVo();

//        private Long cumulativeUsers; // 累计用户数
//        select sum(num_registered) from tb_analysis_by_day // 累计用户数
        Long cumulativeUsers = analysisByDayApi.queryCumulativeUsers();
        analysisSummaryVo.setCumulativeUsers(cumulativeUsers);

//        private Long activePassMonth;//过去30天活跃用户数
//        select sum(num_active) from 	tb_analysis_by_day where record_date>='2021-06-11'  //过去30天活跃用户数
//
//        private Long activePassWeek;//过去7天活跃用户
//        select sum(num_active) from 	tb_analysis_by_day where record_date>='2021-07-04'  //过去7天活跃用户

        String passMonthStr = DateTime.parse(todayStr).minusDays(30).toString("yyyy-MM-dd");
        Long activePassMonth = analysisByDayApi.activePass(passMonthStr);
        analysisSummaryVo.setActivePassMonth(activePassMonth);

        String passWeekStr = DateTime.parse(todayStr).minusDays(7).toString("yyyy-MM-dd");
        Long activePassWeek = analysisByDayApi.activePass(passWeekStr);
        analysisSummaryVo.setActivePassWeek(activePassWeek);

        String yesTodayStr = DateTime.parse(todayStr).minusDays(1).toString("yyyy-MM-dd");

        Long newUsersToday = analysisByDayApi.queryColumAndTime("num_registered",todayStr);
        Long newUsersYesToday =analysisByDayApi.queryColumAndTime("num_registered",yesTodayStr);


//        private Long newUsersToday; //今日新增用户数量
//        select sum(num_registered) from 	tb_analysis_by_day where record_date='2021-07-11' //今日新增用户数量
//        select sum(num_registered) from 	tb_analysis_by_day where record_date='2021-07-10' //昨日新增用户数量
        /**
         * 今日新增用户涨跌率，单位百分数，正数为涨，负数为跌
         *      BigDecimal : 商业数字格式
         *
         */
        //        private BigDecimal newUsersTodayRate;  // 今日新增用户涨跌率 （今天-昨天/昨天）  *100
        analysisSummaryVo.setNewUsersToday(newUsersToday);
        BigDecimal newUsersTodayRate = ComputeUtil.computeRate(newUsersToday.intValue(), newUsersYesToday.intValue());
        analysisSummaryVo.setNewUsersTodayRate(newUsersTodayRate);



//
//        private Long loginTimesToday; //今日登录次数
//        select sum(num_login) from 	tb_analysis_by_day where record_date='2021-07-11' //今日新增用户数量
//        select sum(num_login) from 	tb_analysis_by_day where record_date='2021-07-10' //昨日新增用户数量
//
//        private BigDecimal loginTimesTodayRate;// 今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        Long loginTimesToday = analysisByDayApi.queryColumAndTime("num_login",todayStr);
        Long loginTimesYesToday =analysisByDayApi.queryColumAndTime("num_login",yesTodayStr);

        analysisSummaryVo.setLoginTimesToday(loginTimesToday);
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday.intValue(), loginTimesYesToday.intValue());
        analysisSummaryVo.setLoginTimesTodayRate(loginTimesTodayRate);




     /*   private Long activeUsersToday;//今日活跃用户数量
        select sum(num_active) from 	tb_analysis_by_day where record_date='2021-07-11' //今日新增用户数量
        select sum(num_active) from 	tb_analysis_by_day where record_date='2021-07-10' //昨日新增用户数量

        private BigDecimal activeUsersTodayRate;//今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌*/

        Long activeUsersToday = analysisByDayApi.queryColumAndTime("num_active",todayStr);
        Long activeUsersYesToday =analysisByDayApi.queryColumAndTime("num_active",yesTodayStr);

        analysisSummaryVo.setActiveUsersToday(activeUsersToday);
        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday.intValue(), activeUsersYesToday.intValue());
        analysisSummaryVo.setActiveUsersTodayRate(activeUsersTodayRate);



        return ResponseEntity.ok(analysisSummaryVo);


    }
}
